<template>
  <div class="departments-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="news" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item
                      @click.native="hAdd('')"
                    >添加子部门</el-dropdown-item>

                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>

        <!-- 属性的组件el-tree搭建属性的结构的数据 -->
        <!-- el-tree
          default-expand-all 是否展开所有的子节点
        -->
        <el-tree :data="list" default-expand-all>
          <!-- 用一个行列布局 -->
          <!-- 使用作用域插槽获取组价的数据 -->
          <!-- 作用域插槽 data拿到的是每一个子节点的对象 -->
          <!-- #default === slot-scope
               什么时候用到作用域插槽？
               父组件中如果想使用子组件中的数据进行自定义内容的渲染 (类似于table 单元格数据渲染)
          -->
          <template #default="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%"
            >
              <el-col :span="20">
                <svg-icon icon-class="home" /><span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item
                          v-if="data.children.length ===0"
                          @click.native="hDel(data.id)"
                        >删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <el-dialog
        :title="isEdit ? '编辑部门' : '添加子部门'"
        :visible.sync="showDialog"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        width="50%"
        @closed="hClose"
      >
        <addOreEdit
          :id="curId"
          ref="deptRef"
          :is-edit="isEdit"
          :origin-list="originList"
          @success="hSuccess"
          @close="showDialog=false"
        />
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { getDepartments, delDepartments } from '@/api/departments'
import { transDataToTreeDate } from '@/utils'
import addOreEdit from './deptDialog.vue'
export default {
  components: {
    addOreEdit
  },
  data() {
    return {
      list: [],
      showDialog: false,
      curId: '',
      isEdit: false,
      originList: []
    }
  },
  created() {
    this.loadDepartments()
  },
  methods: {
    hClose() {
      console.log('关闭弹层')
      this.$refs.deptRef.resetForm()
    },
    // 删除部门
    hDel(id) {
      this.$confirm('您确定要删除该部门嘛?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          // 发送请求
          const res = await delDepartments(id)
          if (res.code === 10000) {
            this.$message.success(res.message)
            // 重新渲染
            this.loadDepartments()
          }
        })
        .catch(() => {
          this.$message.info('已取消删除')
        })
    },

    hEdit(id) {
      // console.log(id)
      // 进行赋值
      this.curId = id
      // 解决数据不更新的方式2
      // 保证dom更新完成之后，再调用子组件中的方法，重新获取数据，填充表单
      this.$nextTick(() => {
        this.$refs.deptRef.loadDepartmentsDetail()
      })
      // 打开弹窗
      this.showDialog = true
      // 修改
      this.isEdit = true
    },
    hSuccess() {
      // 重新渲染数据
      this.loadDepartments()
      // 关闭弹窗
      this.showDialog = false
    },
    hAdd(id) {
      // console.log(id)
      // 进行赋值
      this.curId = id
      // 打开弹窗
      this.showDialog = true
      // 添加
      this.isEdit = false
    },
    async loadDepartments() {
      try {
        const res = await getDepartments()
        // console.log(res)
        if (res.code !== 10000) this.$message.error(res.message)
        this.originList = res.data.depts.map(item => {
          const { id, code, name, pid } = item
          return { id, code, name, pid }
        })

        this.$message.success(res.message)
        res.data.depts.shift() // 删除第一条数据，公司名称
        this.list = transDataToTreeDate(res.data.depts)
      } catch (error) {
        // console.log(error)
        this.$message.error('获取数据失败！')
      }
    }
  }
}
</script>
